" ------------------------
" pastemode toggle
" ------------------------
inoremap <M-i> <C-\><C-o>:set paste<Cr>
nnoremap <M-i> :set nopaste! nopaste?<CR>
" ------------------------
" basic yank && paste
" ------------------------
nnoremap <Tab>y :0,-y<Cr>
nnoremap <Tab>Y vGy
function! YankFromBeginning() abort
    let original_cursor_position = getpos('.')
    if has('clipboard')
        exec('normal! v^"*y')
        echo "Yank from line beginning to clipboard."
    else
        exec('normal! v^y')
        echo "Yank from line beginning."
    endif
    call setpos('.', original_cursor_position)
endfunction
nnoremap <silent><Leader>Y :call YankFromBeginning()<Cr>
if has('clipboard')
    if UNIX()
        nnoremap <leader>+ viw"+y
        xnoremap <leader>+ "+y
    else
        nnoremap <leader>+ viw"*y
        xnoremap <leader>+ "*y"
    endif
    " yank
    nnoremap Y "*y$:echo "Yank to the line ending to clipboard"<Cr>
    xnoremap y "*y:echo "Yank selected to clipboard"<Cr>
    nnoremap y "*y
    " paste
    cnoremap <M-v> <C-r>*
    inoremap <M-v> <C-r>*
    " M-x/BS
    nnoremap <M-x> "*x
    xnoremap <M-x> "*x
    nnoremap <M-y> "*X
    xnoremap <M-y> "*X
else
    nnoremap Y y$
    cnoremap <M-v> <C-r>"
    inoremap <M-v> <C-r>"
    " M-x/BS
    nnoremap <M-x> x
    xnoremap <M-x> x
    nnoremap <M-y> X
    xnoremap <M-y> X
endif
inoremap <M-y> <BS>
cnoremap <M-y> <BS>
cnoremap <C-y> <C-r>"
nnoremap <S-Insert> P
xnoremap <S-Insert> P
cnoremap <S-insert> <C-r>"
inoremap <S-Insert> <C-r>"
" del/bs
inoremap <M-x> <Del>
cnoremap <M-x> <Del>
" switch 2 words
xnoremap <M-V> <Esc>`.``gvp``P
" --------------------------
" paste in terminal
" --------------------------
if has('clipboard')
    if g:has_terminal == 1
        tnoremap <M-v> <C-\><C-n>"*pa
    elseif g:has_terminal == 2
        tnoremap <M-v> <C-_>"*
    endif
else
    if g:has_terminal == 1
        tnoremap <M-v> <C-\><C-n>""pa
    elseif g:has_terminal == 2
        tnoremap <M-v> <C-_>""
    endif
endif
" ------------------------------------
" registers plugins for leaderf/fzf
" ------------------------------------
function! Get_registers(...) abort
    let reg_lst = split(Execute('registers'), '\n')
    let lst = []
    for reg in reg_lst[1:]
        if reg_lst[0][0] !=# '-'
            let reg = reg[6:]
        endif
        if Installed('leaderf')
            let reg = reg[:80] . ".........."
        endif
        if reg[0] =~ '+'
            call insert(lst, reg, 0)
        elseif reg[0] =~ '*'
            call insert(lst, reg, 0)
        else
            call add(lst, reg)
        endif
    endfor
    return lst
endfunction
if InstalledFzf() && (get(g:, 'prefer_fzf', UNIX()) || !Installed('leaderf'))
    PlugAddOpt 'fzf-registers'
    nnoremap <silent> <M-v> :FzfRegisterPaste<Cr>
    inoremap <silent> <M-v> <C-o>:FzfRegisterPaste<Cr>
    xnoremap <silent> <M-v> :<C-u>FzfRegisterPasteV<Cr>
elseif Installed('leaderf')
    PlugAddOpt 'leaderf-registers'
    nnoremap <silent><M-v> :LeaderfPaste<Cr>
    inoremap <silent><M-v> <ESC>:LeaderfPasteI<Cr>
    xnoremap <silent><M-v> :<C-u>LeaderfPasteV<Cr>
else
    nnoremap <M-v> :registers<Cr>
endif
" ------------------------
" osc52 yankpost
" ------------------------
if exists("##TextYankPost") && UNIX()
    function! s:raw_echo(str)
        if filewritable('/dev/fd/2')
            call writefile([a:str], '/dev/fd/2', 'b')
        else
            exec("silent! !echo " . shellescape(a:str))
            redraw!
        endif
    endfunction
    function! s:copy() abort
        let c = join(v:event.regcontents,"\n")
        if len(Trim(c)) == 0
            return
        endif
        let c64 = system("base64", c)
        if $TMUX == ''
            let s = "\e]52;c;" . Trim(c64) . "\x07"
        else
            let s = "\ePtmux;\e\e]52;c;" . Trim(c64) . "\x07\e\\"
        endif
        call s:raw_echo(s)
    endfunction
    autocmd TextYankPost * call s:copy()
endif
" ------------------------
" paste
" ------------------------
nnoremap <leader>vp viwp
for i in range(10)
    execute printf('nnoremap <leader>v%s viw"%sp', i , i)
    execute printf('xnoremap <leader>%s "%sp', i , i)
endfor
" ------------------------
" copy to register using <M-c>
" ------------------------
for i in range(26)
    let l_char = nr2char(char2nr('a') + i)
    let u_char = nr2char(char2nr('A') + i)
    exec 'xnoremap <M-c>' . l_char . ' gv"'. l_char . 'y'
    exec 'xnoremap <M-c>' . u_char . ' gv"'. u_char . 'y'
endfor
" Yank a line without leading whitespaces and line break
nnoremap <leader>yu mp_yg_`p
" Copy a line without leading whitespaces and line break to clipboard
nnoremap <leader>yw mp_"+yg_`P
" Copy file path
nnoremap <leader>yp :let @*=Expand("%:p")<cr>:echo '-= File path copied=-'<Cr>
" Copy file name
nnoremap <leader>yf :let @*=Expand("%:t")<cr>:echo '-= File name copied=-'<Cr>
" Copy bookmark position reference
nnoremap <leader>ym :let @*=Expand("%:p").':'.line(".").':'.col(".")<cr>:echo '-= Cursor bookmark copied=-'<cr>'
